package com.mosai.alignpro.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mosai.alignpro.BaseActivity;
import com.mosai.alignpro.R;
import com.mosai.alignpro.entity.ImageInfo;
import com.mosai.alignpro.util.IntentUtils;
import com.mosai.alignpro.util.LogUtils;

import java.util.Collections;
import java.util.List;

/**
 * 诊断列表适配器
 * Created by Rays on 2020/3/9.
 */
public final class DiagnosisListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private static final String TAG = DiagnosisListAdapter.class.getSimpleName();

    public static final String PAYLOAD_DESCRIBE = "PAYLOAD_DESCRIBE";

    private final int width;
    private final int height;
    private final BaseActivity baseActivity;
    private final RequestOptions doctorOptions;
    private final RequestOptions patientOptions;
    private List<ImageInfo> data = Collections.emptyList();
    private int totalCount;
    private boolean isAssistant;
    private boolean isCanConfirm;

    public DiagnosisListAdapter(BaseActivity baseActivity, int width, int height) {
        super();
        this.width = width;
        this.height = height;
        this.baseActivity = baseActivity;
        int doctorRadius = baseActivity.getResources().getDimensionPixelSize(R.dimen.doctor_avatar_size) >> 1;
        doctorOptions = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(doctorRadius));
        int patientRadius = baseActivity.getResources().getDimensionPixelSize(R.dimen.patient_avatar_size) >> 1;
        patientOptions = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(patientRadius));
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (IntentUtils.isPatientApp()) {
            View view = View.inflate(viewGroup.getContext(), R.layout.item_diagnosis_by_patient, null);
            view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
            return new DiagnosisListByPatientVH(view, baseActivity, patientOptions);
        }
        View view = View.inflate(viewGroup.getContext(), R.layout.item_diagnosis, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        return new DiagnosisListViewHolder(view, baseActivity, doctorOptions, patientOptions, this, isAssistant);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        LogUtils.i(TAG, "onBindViewHolder"
                + " position: " + position
                + " payloads: " + payloads
        );
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
            return;
        }
        holder.onBindViewHolder(data.get(position), payloads);
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        LogUtils.i(TAG, "onBindViewHolder position: " + position);
        holder.itemView.setTag(position);
        holder.onBindViewHolder(data.get(position), position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public List<ImageInfo> getData() {
        return data;
    }

    public void setData(List<ImageInfo> data) {
        this.data = data == null ? Collections.emptyList() : data;
    }

    public void setAssistant(boolean assistant) {
        isAssistant = assistant;
    }

    public boolean isCanConfirm() {
        return isCanConfirm;
    }

    public void setCanConfirm(boolean canConfirm) {
        isCanConfirm = canConfirm;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public void add(List<ImageInfo> items) {
        if (items == null) {
            items = Collections.emptyList();
        }
        if (data.isEmpty()) {
            data = items;
        } else {
            data.addAll(items);
        }
    }
}
